package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        menuService.add(menu);
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Menu> pageResult = menuService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    @GetMapping("/findById")
    public Result findById(int id){
        Menu menu = menuService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,menu);
    }

    @GetMapping("/getParentMenuIds")
    public Result getParentMenuIds(int id){
        List<Integer> parentMenuIds = menuService.getParentMenuIds();
        return new Result(true, MessageConstant.QUERY_ParentMenuIds_SUCCESS,parentMenuIds);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Menu menu){
        menuService.update(menu);
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }



    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
        menuService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }



    //查询所有列表
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Menu> menuList = menuService.findAll();
        if(menuList!=null&&menuList.size()!=0)
        {
            Result result = new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menuList);
            return result;
        }else {
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }


    //根据role的id查找
    @RequestMapping("/findMenuIdsByRoleId")
    public List<Integer> findMenuIdsByRoleId(int id){
        List<Integer> list = menuService.findMenuIdsByRoleId(id);
        return list;
    }

}
